package com.czd.support.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.czd.base.model.JsonResult;
import com.czd.support.common.Constants;
import com.czd.support.enums.FileType;
import com.czd.support.helper.AttachHelper;
import com.czd.support.service.AgentAttachmentService;
import com.czd.support.util.FileUtils;

 


@Controller
@RequestMapping("/file")
public class FileuUploadController  extends  BaseController {
	
	
	@Autowired
	private  AgentAttachmentService  agentAttachmentService  ;
	@ResponseBody  
	@RequestMapping(value = "/upload",method = RequestMethod.POST)
	public JsonResult    upload(HttpServletRequest  request)  {
		MultipartHttpServletRequest multipartRequest  =  	(MultipartHttpServletRequest) request;
		MultipartFile attachFile =  multipartRequest.getFile("attachFile");
//		String uploadType = WebUtils.findParamStr(request, "uploadType", "");//上传类型
		String fileName   ="";
		try {
			fileName = new String(attachFile.getOriginalFilename().getBytes(), "UTF-8");
		} catch ( Exception e) {
			e.printStackTrace();   
			return JsonResult.FAIL.put("message", "文件上传失败,请注意文件格式或大小");
		} //转码
		String fileType = FileUtils.getFileType(fileName);
		String rP =fileType.equalsIgnoreCase(FileType.video.toString()) ? Constants.PATH_FILE: Constants.PATH_FILE ;
		String rootPath = rP.substring(1);  
		String url  ="";
		try {
			url = AttachHelper.saveAttach(request, attachFile, rootPath, true);
		} catch (IOException e) {
			e.printStackTrace();
			return JsonResult.FAIL.put("message", "文件上传失败,请注意文件格式或大小");
		}
		return JsonResult.OK.put("url",url);
	 
	}
	 

}
